<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 9:55
 */

namespace bgb\weixin;

use bgb\Http;

/**
 * 微信模板消息
 * wiki https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
 * Class Template
 * @package bgb\weixin
 */
class Template
{

    use Http;

    protected $accessToken; //微信公众号accesstoken

    protected static $instance = [];

    const GET_INDUSTRY_URL = 'https://api.weixin.qq.com/cgi-bin/template/get_industry'; //获取设置的行业信息

    const SET_INDUSTRY_URL = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry'; //设置所属行业

    const GET_TEMPLATE_ID_URL = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template'; //获得模板ID

    const GET_ALL_PRIVATE_TEMPLATE_URL = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template'; //获取模板列表

    const DEL_PRIVATE_TEMPLATE_URL = 'https://api.weixin.qq.com/cgi-bin/template/del_private_template'; //删除模板

    const SEND_TEMPLATE_URL = 'https://api.weixin.qq.com/cgi-bin/message/template/send'; //发送模板消息


    public function __construct($accessToken)
    {
        $this->accessToken = $accessToken;
    }

    /**
     * 单例支持
     * @param $accessToken
     * @return $this
     */
    public static function getInstance($accessToken)
    {
        $key = md5($accessToken);
        if (self::$instance[$key]) {
            self::$instance[$key] = new static($accessToken);
        }

        return self::$instance[$key];
    }

    /**
     * 设置所属行业
     * @param $industryId1
     * @param $industryId2
     * @return mixed
     */
    public function setIndustry($industryId1, $industryId2)
    {
        return $this->_postRequest(self::SET_INDUSTRY_URL, [
            'industry_id1' => $industryId1,
            'industry_id2' => $industryId2
        ]);
    }

    /**
     * 获取设置的行业信息
     * @return mixed
     */
    public function getIndustry()
    {
        return $this->_getRequest(self::GET_INDUSTRY_URL);
    }

    /**
     * 获得模板ID
     * @param $templateIdShort
     * @return mixed
     */
    public function getTemplateId($templateIdShort)
    {
        return $this->_postRequest(self::GET_TEMPLATE_ID_URL, [
            'template_id_short' => $templateIdShort
        ]);
    }

    /**
     * 获取模板列表
     * @return mixed
     */
    public function getTemplateLists()
    {
        return $this->_getRequest(self::GET_ALL_PRIVATE_TEMPLATE_URL);
    }

    /**
     * 删除模板
     * @param $templateId
     * @return mixed
     */
    public function delTemplate($templateId)
    {
        return $this->_postRequest(self::DEL_PRIVATE_TEMPLATE_URL, [
            'template_id' => $templateId
        ]);
    }

    /**
     * 发送模板消息
     * @param $toUser
     * @param $data
     * @param $templateId
     * @param string $url
     * @param array $miniprogram
     * @return mixed
     */
    public function send($toUser, $data, $templateId, $url = '', $miniprogram = [])
    {
        return $this->_postRequest(self::SEND_TEMPLATE_URL, [
            'touser' => $toUser,
            'template_id' => $templateId,
            'data' => $data,
            'url' => $url,
            'miniprogram' => $miniprogram
        ]);
    }

    /**
     * @param $url
     * @param $params
     * @return mixed
     */
    protected function _postRequest($url, $params = [])
    {
        return $this->postRequest($url . '?access_token=' . $this->accessToken, json_encode($params, JSON_UNESCAPED_UNICODE));
    }

    /**
     * @param $url
     * @param $params
     * @return mixed
     */
    protected function _getRequest($url, $params = [])
    {
        return $this->getRequest($url . '?access_token=' . $this->accessToken, $params);
    }
}